package longrise.phone.com.bjjt_jyb.Utils;

import java.util.List;

import com.longrise.android.util.Util;
import com.longrise.android.widget.LLinearLayoutView;
import longrise.phone.com.bjjt_jyb.Application.BaseApplication;
import longrise.phone.com.bjjt_jyb.View.AbstractSpinerAdapter.IOnItemSelectListener;
import longrise.phone.com.bjjt_jyb.View.AlertDialog;
import longrise.phone.com.bjjt_jyb.View.AlertDialogTips;
import longrise.phone.com.bjjt_jyb.View.SpinerPopWindow;
import longrise.phone.com.bjjt_jyb.View.SpinerPopWindow2;


import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import longrise.phone.com.bjjt_jyb.R;
import longrise.phone.com.bjjt_jyb.widget.AlertDialogRecord2;

/**
 * 旧核心库的uiuitils
 */
public class UiUtil
{

	public static final int NETTYPE_WIFI = 0x01;
	public static final int NETTYPE_CMWAP = 0x02;
	public static final int NETTYPE_CMNET = 0x03;
	public static Toast mToast1 = null;

	/**
	 * 显示toast
	 * 
	 * @param context
	 * @param content
	 */
	// public static void showToast(final Context context, String content)
	// {
	// Toast.makeText(context, content, Toast.LENGTH_LONG).show();
	// }
	public static void showToast(final Context context, String content)
	{
		if (mToast1 == null)
		{
			mToast1 = Toast.makeText(context, content, Toast.LENGTH_LONG);

		} else
		{
			mToast1.setText(content);
			mToast1.setDuration(Toast.LENGTH_SHORT);

		}

		mToast1.show();

	}

	/**
	 * 显示下拉选择popwindow
	 * 
	 * @param context
	 *            上下文
	 * @param dataList
	 *            list数据
	 * @param listener
	 *            选择回调函数
	 * @param view
	 *            要显示在哪个view下面
	 */
	public static SpinerPopWindow showSelectPopWindow(Context context, List<String> dataList,
			IOnItemSelectListener listener, View view)
	{
		SpinerPopWindow mSpinerPopWindow = new SpinerPopWindow(context);
		mSpinerPopWindow.refreshData(dataList, 0);
		mSpinerPopWindow.setItemListener(listener);
		mSpinerPopWindow.setWidth(view.getWidth());
		mSpinerPopWindow.setView(view);
		mSpinerPopWindow.showAsDropDown(view);

		return mSpinerPopWindow;
	}
	public static SpinerPopWindow2 showSelectPopWindow2(Context context, List<String> dataList,
			IOnItemSelectListener listener, View view)
	{
		SpinerPopWindow2 mSpinerPopWindow2 = new SpinerPopWindow2(context);
		mSpinerPopWindow2.refreshData(dataList, 0);
		mSpinerPopWindow2.setItemListener(listener);
		mSpinerPopWindow2.setWidth(view.getWidth());
		mSpinerPopWindow2.setView(view);
		mSpinerPopWindow2.showAsDropDown(view);
		return mSpinerPopWindow2;
	}



	/**
	 * 显示下拉选择popwindow 可以通过isWarpContent设置是否包裹内容
	 * 
	 * @param context
	 *            上下文
	 * @param dataList
	 *            list数据
	 * @param listener
	 *            选择回调函数
	 * @param view
	 *            要显示在哪个view下面
	 *
	 * 
	 */
	public static SpinerPopWindow showSelectPopWindow(Context context, List<String> dataList,
			IOnItemSelectListener listener, View view, boolean isWarpConent)
	{
		SpinerPopWindow mSpinerPopWindow = new SpinerPopWindow(context);
		mSpinerPopWindow.setWarpContent(isWarpConent);
		mSpinerPopWindow.refreshData(dataList, 0);
		mSpinerPopWindow.setItemListener(listener);
		mSpinerPopWindow.setWidth(view.getWidth());
		mSpinerPopWindow.setView(view);
		mSpinerPopWindow.showAsDropDown(view);
		return mSpinerPopWindow;
	}
	/**
	 * 显示在控件上方
	 * @param context
	 * @param dataList
	 * @param listener
	 * @param view
	 * @param isWarpConent
	 * @return
	 */
	public static SpinerPopWindow2 showSelectPopWindow2(Context context, List<String> dataList,
			IOnItemSelectListener listener, View view,boolean isWarpConent)
	{
		SpinerPopWindow2 mSpinerPopWindow2 = new SpinerPopWindow2(context);
		mSpinerPopWindow2.setWarpContent(isWarpConent);
		mSpinerPopWindow2.refreshData(dataList, 0);
		mSpinerPopWindow2.setItemListener(listener);
		mSpinerPopWindow2.setWidth(view.getWidth());
		mSpinerPopWindow2.setView(view);
		mSpinerPopWindow2.showAsDropDown(view);
		return mSpinerPopWindow2;

	}

	/**
	 * 选择对话框
	 * 
	 * @param context
	 *            上下文
	 * @param title
	 *            标题
	 * @param content
	 *            内容
	 * @param leftStr
	 *            左边btn内容
	 * @param rightStr
	 *            右边btn内容
	 * @param leftClick
	 * @param rightClick
	 */
	public static AlertDialog showDialog(Context context, String title, String content, String leftStr, String rightStr,
			OnClickListener leftClick, OnClickListener rightClick)
	{
		final AlertDialog dialog = new AlertDialog(context).builder();
		dialog.setTitle(title);
		dialog.setShowTitle(true);
		dialog.setShowEditText(false);
		dialog.setMsg(content);
		dialog.setNegativeButton(leftStr, leftClick);
		dialog.setPositiveButton(rightStr, rightClick);
		dialog.show();
		return dialog;
	}

	/**
	 * 单按钮强制跳转的对话框
	 *
	 */
	public static AlertDialog showSingleDialog(Context context, String content, String login,
			OnClickListener rightClick)
	{
		final AlertDialog dialog = new AlertDialog(context).builder();
		dialog.setTitle("提示");
		dialog.setShowTitle(true);
		dialog.setShowEditText(false);
		dialog.setMsg(content);
		dialog.setPositiveButton(login, rightClick);
		dialog.setCancelable(false);
		dialog.show();
		return dialog;
	}

	/**
	 * 确定取消提示对话框
	 * 
	 * @param context
	 * @param content
	 * @param leftClick
	 * @param rightClick
	 */
	public static AlertDialog showDialog(Context context, String content, OnClickListener leftClick,
			OnClickListener rightClick)
	{
		return showDialog(context, "提示", content, "取消", "确定", leftClick, rightClick);
	}

	/**
	 * 
	 * @param context
	 *            上下文
	 * @param isSingleBtn
	 *            标题
	 * @param content
	 *            内容
	 * @param leftStr
	 *            左边btn内容
	 * @param rightStr
	 *            右边btn内容
	 * @param leftClick
	 *            左边事件
	 * @param rightClick
	 *            右边事件
	 */
	public static AlertDialogTips showDialogTips(Context context, boolean isSingleBtn, String content, String leftStr,
			String rightStr, OnClickListener leftClick, OnClickListener rightClick)
	{
		final AlertDialogTips dialog = new AlertDialogTips(context).builder();
		dialog.isSingleConfirm(isSingleBtn);
		dialog.setCancelable(false);
		dialog.setContent(content);
		dialog.setNegativeButton(leftStr, leftClick);
		dialog.setPositiveButton(rightStr, rightClick);
		dialog.show();
		return dialog;
	}

	/**
	 * 显示waiting对话框
	 * 
	 * @param text
	 */
	public static Dialog showProcessDialog(Context context, String text)
	{
		Dialog processDialog = new Dialog(context, R.style.dialog_style);//如果想转圈时可以返回就改完Dialog,AlertDialogRecord2
		processDialog.setCanceledOnTouchOutside(false);

		LLinearLayoutView dialogView = new LLinearLayoutView(context);
		dialogView.setBackgroundColor(Color.parseColor("#55000000"), -10, -10, -10, -10, Util.dip2px(context, 5), 0,
				Color.parseColor("#55000000"));
		dialogView.setGravity(Gravity.CENTER);
		dialogView.setOrientation(LinearLayout.VERTICAL);

		// 进度条
		ProgressBar bar = new ProgressBar(context);
		bar.setIndeterminate(true);
		bar.setIndeterminateDrawable(context.getResources().getDrawable(R.drawable.lcontainerview_anim_loading));
		int w = Util.dip2px(context, 30);
		dialogView.addView(bar, w, w);

		TextView textView = new TextView(context);
		textView.setText(text);
		textView.setGravity(Gravity.CENTER);
		textView.setTextSize(14);
		textView.setTextColor(Color.parseColor("#FFFFFF"));
		textView.setPadding(0, Util.dip2px(context, 10), 0, 0);
		dialogView.addView(textView);

		int widthDialog = Util.dip2px(context, 100);
		LayoutParams dialogLP = new LayoutParams(widthDialog, widthDialog);
		processDialog.setContentView(dialogView, dialogLP);
		// add by huxin
		processDialog.show();
		//

		dialogLP = null;

		return processDialog;
	}

	// Toast mToast = null;
	//
	// /** 安全toast，并且多次点击只弹出一次 */
	// public void showSafeToast(final Context context, final String msg)
	// {
	//
	// if ("main".equals(Thread.currentThread().getName()))
	// {
	//
	// if (mToast == null)
	// {
	// mToast = Toast.makeText(context, msg, Toast.LENGTH_LONG);
	// mToast.setGravity(Gravity.CENTER, 0, 0);
	// } else
	// {
	// mToast.setText(msg);
	// mToast.setDuration(3000);
	// mToast.setGravity(Gravity.CENTER, 0, 0);
	// }
	//
	// mToast.show();
	//
	// } else
	// {
	// ((Activity) context).runOnUiThread(new Runnable()
	// {
	// @Override
	// public void run()
	// {
	//
	// if (mToast == null)
	// {
	// mToast = Toast.makeText(context, msg, Toast.LENGTH_LONG);
	// mToast.setGravity(Gravity.CENTER, 0, 0);
	// } else
	// {
	// mToast.setText(msg);
	// mToast.setDuration(Toast.LENGTH_LONG);
	// mToast.setGravity(Gravity.CENTER, 0, 0);
	// }
	//
	// mToast.show();
	// }
	// });
	// }
	//
	// }

	/** 得到上下文 */
	public static Context getContext()
	{
		return BaseApplication.getContext();
	}

	/** 得到Resouce对象 */
	public static Resources getResource()
	{
		return getContext().getResources();
	}

	/** 得到String.xml中的字符串 */
	public static String getString(int resId)
	{
		return getResource().getString(resId);
	}

	/** 得到String.xml中的字符串数组 */
	public static String[] getStringArr(int resId)
	{
		return getResource().getStringArray(resId);
	}

	/** 得到colors.xml中的颜色 */
	public static int getColor(int colorId)
	{
		return getResource().getColor(colorId);
	}

	/** 得到应用程序的包名 */
	public static String getPackageName()
	{
		return getContext().getPackageName();
	}

	/**
	 * 获取版本号
	 * @return
	 */
	public static int getVersionCode(){
		PackageManager manager = getContext().getPackageManager();//获取包管理器
		try {
			//通过当前的包名获取包的信息
			PackageInfo info = manager.getPackageInfo(getPackageName(),0);//获取包对象信息
			return  info.versionCode;
		} catch (PackageManager.NameNotFoundException e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * dip-->px
	 */
	public static int dip2Px(int dip)
	{
		// px/dip = density;
		float density = getResource().getDisplayMetrics().density;
		int px = (int) (dip * density + .5f);
		return px;
	}

	/**
	 * px-->dip
	 */
	public static int px2Dip(int px)
	{
		// px/dip = density;
		float density = getResource().getDisplayMetrics().density;
		int dip = (int) (px / density + .5f);
		return dip;
	}

	Toast mToast = null;

	/** 安全toast，并且多次点击只弹出一次 */
	public void showSafeToast(final Context context, final String msg)
	{

		if ("main".equals(Thread.currentThread().getName()))
		{

			if (mToast == null)
			{
				mToast = Toast.makeText(context, msg, Toast.LENGTH_LONG);
				mToast.setGravity(Gravity.CENTER, 0, 0);
			} else
			{
				mToast.setText(msg);
				mToast.setDuration(Toast.LENGTH_SHORT);
				mToast.setGravity(Gravity.CENTER, 0, 0);
			}

			mToast.show();

		} else
		{
			((Activity) context).runOnUiThread(new Runnable()
			{
				@Override
				public void run()
				{

					if (mToast == null)
					{
						mToast = Toast.makeText(context, msg, Toast.LENGTH_LONG);
						mToast.setGravity(Gravity.CENTER, 0, 0);
					} else
					{
						mToast.setText(msg);
						mToast.setDuration(Toast.LENGTH_LONG);
						mToast.setGravity(Gravity.CENTER, 0, 0);
					}

					mToast.show();
				}
			});
		}

	}

	/**
	 * 网络是否连接
	 * 
	 * @return
	 */
	public static boolean isNetworkConnected()
	{
		ConnectivityManager cm = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo ni = cm.getActiveNetworkInfo();
		return ni != null && ni.isConnectedOrConnecting();
	}

	/**
	 * 
	 * 得到网络类型
	 * 
	 * @return
	 */
	public static int getNetworkType()
	{
		int netType = 0;
		ConnectivityManager connectivityManager = (ConnectivityManager) getContext()
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
		if (networkInfo == null)
		{
			return netType;
		}
		int nType = networkInfo.getType();
		if (nType == ConnectivityManager.TYPE_MOBILE)
		{
			String extraInfo = networkInfo.getExtraInfo();
			if (!TextUtils.isEmpty(extraInfo))
			{
				if (extraInfo.toLowerCase().equals("cmnet"))
				{
					netType = NETTYPE_CMNET;
				} else
				{
					netType = NETTYPE_CMWAP;
				}
			}
		} else if (nType == ConnectivityManager.TYPE_WIFI)
		{
			netType = NETTYPE_WIFI;
		}
		return netType;
	}

	/**
	 * 应用包信息
	 * 
	 * @return
	 */
	public static PackageInfo getPackageInfo()
	{
		PackageInfo info = null;
		try
		{
			info = getContext().getPackageManager().getPackageInfo(getPackageName(), 0);
		} catch (NameNotFoundException e)
		{
			e.printStackTrace(System.err);
		}
		if (info == null)
			info = new PackageInfo();
		return info;
	}

	/**
	 * 
	 * 获取设备的MacAddress地址
	 * 
	 * @return
	 */
	public static String getLocalMacAddress()
	{
		TelephonyManager tm = null;
		try
		{
			tm = (TelephonyManager) getContext().getSystemService(Context.TELEPHONY_SERVICE);
			if (null != tm)
			{
				return tm.getDeviceId();
			}
		} catch (Exception ex)
		{
		} finally
		{
			tm = null;
		}
		return null;
	}


	public static android.support.v7.app.AlertDialog showMainDialog(Context context, String title, String message, String negebutton,
																	String posibutton, DialogInterface.OnClickListener listener) {
		android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(context, R
				.style.AlertDialogStyle);
		android.support.v7.app.AlertDialog alertDialog = builder.setTitle(title)
				.setMessage(message)
				.setNegativeButton(negebutton, null)
				.setPositiveButton(posibutton, listener)
				.create();

		return alertDialog;
	}

}
